import 'package:flutter/material.dart';
import 'dart:math';
import 'package:note_life/theme.dart';

class CustomerProgressView extends CustomPainter {
  Paint bgPaint;
  Color bgColor = ThemeUtil.type == ThemeUtil.BRIGHT_THEME
      ? Color(0X44000000)
      : Color(0X44FFFFFF);
  Color pointColor = Colors.red;
  Paint pointPaint;
  Paint progressPaint;
  Paint shadowPaint;
  double curNumber = 1.0;
  double maxNumber = 100.0;

  CustomerProgressView(this.curNumber);

  @override
  void paint(Canvas canvas, Size size) {
    if (curNumber < 0) {
      curNumber = 1.0;
    }
    bgPaint = Paint()
      ..style = PaintingStyle.stroke
      ..color = bgColor
      ..strokeWidth = 4.0;
    pointPaint = Paint()
      ..style = PaintingStyle.fill
      ..color = pointColor;
    Offset offsetCenter = Offset(30.0, 30.0);
    canvas.drawCircle(offsetCenter, 30.0, bgPaint);
    var rect = Rect.fromCircle(center: offsetCenter, radius: 30.0);
    var angle = 360.0 * (curNumber / maxNumber);
    double radians = degToRad(angle);
    Gradient gradient = new SweepGradient(
        colors: [Color(0XFF1FA7BC), Color(0XFF029B83)], endAngle: radians);
    progressPaint = Paint()
      ..style = PaintingStyle.stroke
      ..strokeCap = StrokeCap.round
      ..strokeWidth = 6.0
      ..shader = gradient.createShader(rect);
    shadowPaint = Paint()
      ..color = ColorTween(begin: Colors.black,end: Colors.transparent).lerp(0.8)
      ..style = PaintingStyle.stroke
      ..strokeCap = StrokeCap.round
      ..strokeWidth = 2.0;
    canvas.save();
    canvas.translate(0.0, 60.0);
    canvas.rotate(degToRad(-90.0));
    var offset = asin(6 * 0.5 / 30.0);
//    Offset shadowOffsetCenter = Offset(30.0, 30.0);
//    var shadpwRect = Rect.fromCircle(center: shadowOffsetCenter, radius: 33.0);
//    if(curNumber/maxNumber>0.03&&curNumber/maxNumber<0.75){
//      var shadowStart = maxNumber*0.05;
//      var shadowEnd = curNumber<maxNumber*0.75?curNumber:maxNumber*0.75;
//      var startAngle = 360.0 * (shadowStart / maxNumber);
//      var endAngle = 360.0 * (shadowEnd / maxNumber);
//      print("start:${shadowStart},end:${degToRad(endAngle)}");
//      canvas.drawArc(shadpwRect, degToRad(startAngle) ,degToRad(endAngle-startAngle) , false, shadowPaint);
//    }
    canvas.drawArc(rect, offset,  radians - offset, false, progressPaint);
    canvas.restore();
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }

  num degToRad(num deg) => deg * (pi / 180.0);
}
